/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.ejemplo.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Camila
 */
public class Conexion {
    private Connection cn;
    private ResultSet rs;
    
    /**
     *
     */
    public Conexion()
    {
         
    }   
    private void conectar()
    {
      try {
            Class.forName("com.mysql.jdbc.Driver");
            
            cn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");
            System.out.println("Conexion establecida");
        } catch (Exception e) {
            System.out.println("Conexion::conectar::error->" + e);
        }              
    }   
    public void desconectar()
    {
        try {
            cn.close();
        } catch (SQLException ex) {
            System.out.println("Conexion::desconectar::error->" + ex);
        }
    }    

    /**
     *
     * @param sql
     * @return
     */
    public ResultSet ObtenerConsulta(String sql)
    {
        ResultSet rs=null;
        try {
            
            conectar();
            Statement stmt=cn.createStatement();
            rs=stmt.executeQuery(sql);
            
        } catch (SQLException ex) {
             System.out.println("Conexion::ObtenerConsulta::error->" + ex);
              System.out.println("Conexion::ObtenerConsulta::sql->" + sql);
             
        }
        
        return rs;
        
    }

    /**
     *
     * @param sql
     */
    public void ejecutaSql(String sql)
    {
        try {
            conectar();
            Statement stmt=cn.createStatement();
            stmt.executeUpdate(sql);
            
        } catch (SQLException ex) {
             System.out.println("Conexion::ObtenerConsulta::error->" + ex);
              System.out.println("Conexion::ObtenerConsulta::sql->" + sql);
             
        }

    }
    public void finStmt(){
      
    }
        
}
